import {routerRedux} from 'dva/router';


var auth  = require('../tools/auth-tool');

export default {
  namespace : 'index',

  state:{
    loading:false
  },

  subscriptions:{
    setup({dispatch}){
      console.log('app setup');
      dispatch({type:'loadAuthFromLocalStorage'});
    }
  },

  effects:{
    *loadAuthFromLocalStorage(_, {put}){
      auth.loadAuthData();
      console.log('Load auth data from local storage. Token is:', auth.getToken());

      if(auth.getToken()){
        console.log("Token exist. Will go to home page");
        yield put(routerRedux.push('/workspace/reception'));
        yield put({type:'project/queryProjectInfos'});
      }
      else{
        console.log("Token not exist. Will go to login page");
        yield put(routerRedux.push('/login'));
      }
    }
  },

  reducers:{

  }
}
